package com.java110.community.cmd.ownerRepair;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.community.cmd.community.UpdateCommunityCmd;
import com.java110.community.dao.impl.RepairServiceDaoImpl;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairStateEnum;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairSettingInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.vo.api.repair.StaffVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Java110Cmd(serviceCode = "ownerRepair.saveOwnerRepair")
public class SaveOwnerRepairCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(UpdateCommunityCmd.class);
    @Autowired
    private IRepairInnerServiceSMO repairInnerService;
    @Autowired
    private IRepairSettingInnerServiceSMO repairSettingInnerService;
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        logger.info("保存保修配置请求入参：{}", reqJson.toJSONString());
        //保存保修单
        Map<String, Object> map = reqJson.getInnerMap();
        RepairDto repairDto = BeanConvertUtil.covertBean(map, RepairDto.class);
        //获取派单方式
        Long repairType = repairDto.getRepairType();
        JSONArray repairSettings = reqJson.getJSONArray("repairSettings");
        String repairWay = null;
        for (Object repairSetting : repairSettings) {
            JSONObject jsonObject = BeanConvertUtil.beanCovertJson(repairSetting);
           if(jsonObject.getLong("id").equals(repairType)){
               repairWay = jsonObject.getString("repairWay");
               break;
           }
        }
        repairDto.setCreatTime(DateUtil.getCurrentDate());
        if("100".equals(repairWay)){
            //抢单
        }else if("200".equals(repairWay)){
            //指派
            repairDto.setState(RepairStateEnum.WAITING.getState());
            repairDto.setStateName(RepairStateEnum.WAITING.getStateName());
            Long id = repairInnerService.saveOwnerRepair(repairDto);
            //添加流程
            reqJson.put("state",RepairStateEnum.WAITING.getState());
            reqJson.put("stateName",RepairStateEnum.WAITING.getStateName());
            reqJson.put("repairId",id);
        }else if ("300".equals(repairWay)){
            //轮询
            //马上派一个师傅
            repairDto.setState(RepairStateEnum.ORDER_TAKING.getState());
            repairDto.setStateName(RepairStateEnum.ORDER_TAKING.getStateName());
            //获取师傅的信息
            List<StaffVo> staffVos = repairSettingInnerService.listRepairTypeUsers(reqJson);
            StaffVo staffVo = staffVos.get(0);
            repairDto.setStaffId(staffVo.getStaffId());
            repairDto.setStaffName(staffVo.getStaffName());
            Long id = repairInnerService.saveOwnerRepair(repairDto);
            //添加流程
            reqJson.put("state",RepairStateEnum.ORDER_TAKING.getState());
            reqJson.put("stateName",RepairStateEnum.ORDER_TAKING.getStateName());
            reqJson.put("repairId",id);
        }
        reqJson.put("currentUserId",reqJson.get("loginUserId"));
        reqJson.put("createUserName",reqJson.get("repairObjName"));
        repairInnerService.repairDispatchFlow(reqJson);
    }
}
